$(document).ready(function() {
	// 滚动加载
	$(document.body).infinite();
	var loading = false; // 状态标记
	var totalPage = $("#total_pages").val();
	$(document.body).infinite().on("infinite", function() {
		if (loading)
			return;
		var page = $("#page").val();
		if (page == null || page == "") {
			 page=0;
		}
		if (parseInt(page)+1 == totalPage)
			return;
		loading = true;
		$("#loading-div").show();
		var sort = $("#sort_input").val();
		var goodsId = $("#goods_id").val();
		getContent(goodsId, parseInt(page) + 1, sort);
	});
	//下拉刷新
	$('#content').css('height', $(window).height());
	$("#content").pullToRefresh();
	$("#content").on("pull-to-refresh", function() {
		var goodsId = $("#goods_id").val();
		getContent(goodsId, 0, "", true);
		$("#content").pullToRefreshDone();
	});
	
	function getContent(goodsId,page,sort,isRefresh){
		if(isRefresh){
			$.showLoading();
		}
		$.ajax({
			url : appPath + '/comments/list.html',
			dataType : 'html',
			data : {
				'goodsId' : goodsId,
				'page' : parseInt(page),
				'sort' : sort
			},
			success : function(data) {
				$("#page").val(parseInt(page));
				var div =$(data).find("#comments_div");
				var appendHtml = $(div[0]).html();
				if(isRefresh){
					$.hideLoading();
					$("#comments_div").html(appendHtml);  
				}else{
					$("#loading-div").hide();
					$("#comments_div").append(appendHtml);  
					loading = false;
				}
			},error:function(msg){
				$.alert("系统异常！");
			}
		});
	}
	
});